package com.example.base.fragment;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.base.R;
import com.example.base.activity.WebViewActivity;
import com.example.base.adapter.FlowAdapter;
import com.example.base.adapter.MyAdapter;
import com.example.base.adapter.SystemAdapter;
import com.example.base.bean.HomeThree;
import com.example.base.bean.Object;
import com.example.base.bean.SysOne;
import com.example.base.bean.SysThree;
import com.example.base.bean.SysTwo;
import com.example.base.bean.SystemTwo;
import com.example.base.sqllite.MySQLite;
import com.example.base.view.BannerViewPager;
import com.example.base.view.NestedListview;
import com.example.base.view.SysViewLayout;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SystemTwoFragment extends Fragment {
  private Context context;
  private ListView listView;
  private SystemAdapter systemAdapter;
  private List<SysThree> dataList = new ArrayList<>();
  private String sys = "https://www.wanandroid.com/article/list/0/json?cid=";
  private int sysright;

  public SystemTwoFragment() {

  }


  @Override
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {

    View inflate = inflater.inflate(R.layout.fragment_system_two, container, false);
    initView(inflate);
    initSystemView();
    sendRequestWithokhttp();
    initListener();
    return inflate;
  }

  private void initView(View inflate) {
    listView = inflate.findViewById(R.id.listview_sys_two);
    context = getContext();
    sysright = (int) getArguments().get("ID");
    Log.i("TAG", sysright + "");
  }

  private void sendRequestWithokhttp() {
    OkHttpClient mOkHttpClient = new OkHttpClient();
    Request request = new Request.Builder()
        .url(sys + sysright)
        .get()
        .build();
    Call call = mOkHttpClient.newCall(request);
    call.enqueue(new Callback() {
      @Override
      public void onFailure(@NotNull Call call, @NotNull IOException e) {
      }

      @Override
      public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
        if (response.isSuccessful()) {
          Gson gson = new Gson();
          String htmlStr = response.body().string();
          SysOne sysOne = gson.fromJson(htmlStr, SysOne.class);
          getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
              listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                  List<SysThree> dataList = systemAdapter.getDataList();
                  ContentValues cv = new ContentValues();
                  cv.put("title", dataList.get(position).getShareUser());
                  cv.put("text", dataList.get(position).getTitle());
                  cv.put("link", dataList.get(position).getLink());
                  SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                  cv.put("time", s.format(new Date()));


                  MySQLite user = new MySQLite(context, "user", null, 1);
                  SQLiteDatabase db = user.getWritableDatabase();

                  long row = db.insert("history", null, cv);
                  Log.i("homefragment", row + "");
                  db.close();
                  Intent intent = new Intent(context, WebViewActivity.class);
                  intent.putExtra("Url", sysOne.getData().getDatas().get(position).getLink());
                  startActivity(intent);
                }
              });
              refreshListView(sysOne);
            }
          });
        }
      }
    });
  }

  private void refreshListView(SysOne sysOne) {
    List<SysThree> data = sysOne.getData().getDatas();
    systemAdapter.setData(data);
    systemAdapter.notifyDataSetChanged();
  }

  private void initSystemView() {
    systemAdapter = new SystemAdapter(context, dataList);
    listView.setAdapter(systemAdapter);
  }

  private void initListener() {

  }


}